package me.boot.auth.jap.controller;

import com.fujieid.jap.core.JapUserService;
import com.fujieid.jap.core.config.AuthenticateConfig;
import com.fujieid.jap.core.config.JapConfig;
import com.fujieid.jap.oauth2.Oauth2GrantType;
import com.fujieid.jap.oauth2.Oauth2ResponseType;
import com.fujieid.jap.oidc.OidcConfig;
import com.fujieid.jap.oidc.OidcStrategy;
import me.boot.auth.jap.config.AuthProperties;
import me.boot.auth.jap.config.AuthStrategy;
import me.zhyd.oauth.utils.UuidUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 使用标准的第三方OIDC服务（基于OAuth2服务）,如OKTA
 * 需要依赖 jap-oidc 模块
 */
@RestController
@RequestMapping("/oidc")
public class OidcController extends BaseAuthController<OidcStrategy> {

    @Resource(name = "oauth2")
    private JapUserService japUserService;

    @Override
    protected AuthStrategy getAuthStrategy() {
        return AuthStrategy.oidc;
    }

    @Override
    protected JapUserService getUserService() {
        return japUserService;
    }

    @Override
    protected OidcStrategy getJapStrategy(JapUserService japUserService, JapConfig japConfig) {
        return new OidcStrategy(japUserService, japConfig);
    }

    @Override
    protected AuthenticateConfig getAuthenticateConfig(AuthProperties.Platform platform) {
        return new OidcConfig()
                .setIssuer(platform.getIssuer())
                .setPlatform(platform.getPlatformId())
                .setState(UuidUtils.getUUID())
                .setClientId(platform.getClientId())
                .setClientSecret(platform.getClientSecret())
                .setCallbackUrl(platform.getCallbackUrl())
                .setScopes(platform.getScopes())
                .setResponseType(Oauth2ResponseType.CODE)
                .setGrantType(Oauth2GrantType.AUTHORIZATION_CODE);
    }

}
